package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.WarningTemplateDAO;
import cn.edu.nju.software.greencreditsystem.domain.WarningTemplate;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.util.List;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-8
 * 类描述：警告类型DAO实现
 */
public class WarningTemplateDAOImpl implements WarningTemplateDAO
{

    @Override
    public boolean save(WarningTemplate wt)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.save(wt);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(WarningTemplate wt)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.delete(wt);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(WarningTemplate wt)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.update(wt);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public WarningTemplate getById(int id)
    {
        WarningTemplate result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (WarningTemplate) s.get(WarningTemplate.class, id);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<WarningTemplate> getAll()
    {
        List<WarningTemplate> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = s.createQuery("from WarningTemplate order by id asc").list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
    
}
